/*
 * dl.h
 *
 *  Created on: 24/giu/2014
 *      Author: Pasquale
 */

#ifndef DL_H_
#define DL_H_

#include "nRF24L01P.h"


#define JOIN_DEFAULT 0

/* Dimensioni Pacchetto DL e Frame */
#define MAX_DL_LENGTH 			1856
#define PAYLOAD_DL_LENGTH  		29
#define HEADER_DL_LENGTH    	3
#define FRAGNUM_MASK			0x3f
#define PIPE_WIDTH_STRUCTURE 	0x04

/* Offset Header */
#define DEST_ADDR_OFFSET	0
#define FRAM_NUM_OFFSET		1
#define RESERVED_OFFSET		2
#define LENGTH_MSB_OFF		3
#define LENGTH_LSB_OFF		4

/* TxPacket Result */
#define SUCCESS				0
#define FRAME_LOST			1
#define LENGTH_ERROR		2

/* RxPacket Result */
#define REFUSED				2
#define PROCESSED			1
#define	ACCEPTED			0


typedef struct{
	uint8_t DLRxBufferPipe[MAX_DL_LENGTH];
	uint16_t packetLength;
	uint8_t numFrameAttese;
	uint8_t numFrameRicevute;
	uint8_t rxMutex;
	uint8_t rxResult;
	uint8_t txAddress;
}rxStructure;

void initDL_nRF24L01P();
void resetBuffer(uint8_t* pbuff, uint16_t length);
void printBuffer(uint8_t* pbuff, uint16_t length);
void preparePayload(uint8_t* packTx, uint8_t* pdata, uint8_t header_length, uint8_t max, uint16_t dataOffset);
void stripPayload(uint8_t* buffDest, uint8_t* buffSource, uint16_t offsetDest, uint16_t offsetSource, uint8_t numByte);
uint8_t TxPacket(uint8_t* pdata, uint16_t length, uint8_t destAddress);
uint8_t RxFrame(uint8_t* pdata, uint8_t length, uint8_t pipeNumber);
uint8_t RxPacket(uint8_t* pdata, uint8_t sourceAddress);
uint8_t RxBroadcast(uint8_t *pdata, uint8_t length);

#endif /* DL_H_ */
